import { useEffect, useState } from "react";
import ReactDOM from "react-dom";

export default function App() {
  const [count, setCount] = useState(0);
  function onScroll() {
    console.log("页面滚动了");
  }

  useEffect(() => {
    window.addEventListener("scroll", onScroll);
    return () => {
      window.removeEventListener("scroll", onScroll);
    };
  }, []);

  useEffect(() => {
    const timerId = setInterval(() => {
      setCount((count) => {
        const newCount = count + 1;
        document.title = newCount;
        return newCount;
      });
    }, 1000);
    return () => {
      clearTimeout(timerId);
    };
  }, []);

  return (
    <div>
      <span>{count}</span>
      <button
        onClick={() => {
          ReactDOM.unmountComponentAtNode(document.getElementById("root"));
        }}
      >
        卸载组件
      </button>
    </div>
  );
}
